<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_message extends MY_Controller {

	function Admin_message()
	{
		parent::MY_Controller();
        parent::check_admin_login();
	}
	
	function message_list($offset=0){
		$this->load->model('Mmessage');

        $total = $this->Mmessage->get_message_count();

		$this->load->library('pagination');

		$config['base_url'] = base_url().'admin_message/message_list/';
		$config['total_rows'] = $total;
		$config['per_page'] = 10;
		$config['uri_segment'] = 3;
		$config['num_links'] = 3;
		$config['first_link'] = '第一页';
		$config['last_link'] = '最后页';
		$config['next_link'] = '&gt;';
		$config['prev_link'] = '&lt;';

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();

        $messages = $this->Mmessage->get_message_list($offset,$config['per_page']);
		$this->template->view('admin/message_list',array('messages'=>$messages,'page_html'=>$page_html));
	}
	
	function message_reply($id){
		$this->load->model('Mmessage');
		$message = $this->Mmessage->get_message_info($id);
		$this->template->view('admin/message_reply',array('message'=>$message));
	}
	
	function message_reply_submit(){
		$id = $this->input->post('id');
		$reply = $this->input->post('reply');
		$this->load->model('Mmessage');
		if($this->Mmessage->update_message_data($id,array('reply'=>$reply))){
			echo '回复成功！';
		} else {
			echo '失败，请重试！';
		}
	}
	
	function message_edit($id){
		$this->load->model('Mmessage');
		$message = $this->Mmessage->get_message_info($id);
		$this->template->view('admin/message_edit',array('message'=>$message));
	}
	
	function message_modify(){
		$id = $this->input->post('id');
		$form = array('title','content','reply');
		$data = $this->input->get_data($form);
		$this->load->model('Mmessage');
		if($this->Mmessage->update_message_data($id,$data)){
			echo '修改成功！';
		} else {
			echo '失败，请重试！';
		}
	}
	
	function message_del($id){
		$data = array('state'=>0);
		$this->load->model('Mmessage');
		if($this->Mmessage->update_message_data($id,$data)){
			echo '删除成功！';
		} else {
			echo '失败，请重试！';
		}
	}
	
}

?>